#include "tasktimer.h"

taskTimer::taskTimer(QObject *parent)
    :QObject(parent),
      m_start_btn(nullptr),
      m_stop_btn(nullptr),
      m_today_count_total(nullptr),
      m_lcdNumber(nullptr),
      m_timer(nullptr),
      m_task_state(WORK),
      m_state_label(nullptr),
      m_timer_end_effect(new QSoundEffect)
{


    // 计时器到期提示音

    m_timer_end_effect->setSource(QUrl::fromLocalFile(taskItem::config->m_bellCurrent));
    m_timer_end_effect->setLoopCount(taskItem::config->m_bellLoop);
    m_timer_end_effect->setVolume(0.7f);



    connect(m_timer, &QTimer::timeout, this, &taskTimer::timerTick);
}

taskTimer::taskTimer(QLabel *today_count_total, QLCDNumber *lcdNumber, QTimer *timer, QLabel *state_label, QPushButton *start_btn, QPushButton *stop_btn, QObject *parent)
    :QObject(parent),
      m_start_btn(start_btn),
      m_stop_btn(stop_btn),
      m_today_count_total(today_count_total),
      m_lcdNumber(lcdNumber),
      m_timer(timer),
      m_task_state(WORK),
      m_state_label(state_label),
      m_timer_end_effect(new QSoundEffect)
{


    connect(m_timer, &QTimer::timeout, this, &taskTimer::timerTick);

}

bool taskTimer::isActivate()
{
    return m_timer->isActive();
}

void taskTimer::lcdDisplay(int count)
{
    int minute = count / 60;
    int sec = count % 60;

    char buf[10];
    sprintf(buf, "%02d:%02d", minute, sec);

    m_lcdNumber->display(buf);
}



void taskTimer::resetCount()
{
    m_task_state == WORK? m_counts = taskItem::config->m_tomatoTime:m_counts = taskItem::config->m_tomatoTime_rest;
}

void taskTimer::stateSwitch()
{
    if (m_task_state == WORK){
        m_task_state = REST;
        m_counts = taskItem::config->m_tomatoTime_rest;
        m_state_label->setText("休息");
    } else {
        m_task_state = WORK;
        m_counts = taskItem::config->m_tomatoTime;
        m_state_label->setText("工作");
    }
}

void taskTimer::Init()
{
    m_task_state = WORK;
    TaskReset();
}



void taskTimer::TaskStart()
{
    if (m_timer->isActive() == false) {
        m_timer->start(1000);   //开启定时器，频率为1000ms
        lcdDisplay(m_counts);

        m_time_vec.first = QTime::currentTime();
        m_time_vec_flag = true;

        m_start_btn->setDisabled(true);
        m_stop_btn->setDisabled(false);
    }

}

void taskTimer::TaskPause()
{
    if (m_timer->isActive() == true){
        m_timer->stop();
        lcdDisplay(m_counts);

        if (m_time_vec_flag)
        {
            m_time_vec_flag = false;
            m_time_vec.second = QTime::currentTime();
            m_time_vecs.push_back(m_time_vec);

        }


        m_start_btn->setDisabled(false);
        m_stop_btn->setDisabled(true);
    }

}

void taskTimer::TaskReset()
{
    if (m_timer->isActive() == true) m_timer->stop();
    resetCount();
    lcdDisplay(m_counts);

    m_time_vec_flag = false;
    m_time_vecs.clear();

    m_start_btn->setDisabled(false);
    m_stop_btn->setDisabled(true);
}

void taskTimer::TaskEnd()
{
    m_time_vec.second = QTime::currentTime();
    m_time_vecs.push_back(m_time_vec);
    m_time_vec_flag = false;




    if (m_timer->isActive() == true) m_timer->stop();
    timeVecs *ptimeVecs = new timeVecs(m_time_vecs);
    m_time_vecs.clear();

    bellPlay();
    stateSwitch();

    lcdDisplay(m_counts);

    m_start_btn->setDisabled(false);
    m_stop_btn->setDisabled(true);

    emit SendTaskEndSignal(ptimeVecs);
}

void taskTimer::timerTick()
{
    m_counts--;
    if (m_counts) {
        lcdDisplay(m_counts);

    } else
        TaskEnd();
}

void taskTimer::bellPlay()
{
    m_timer_end_effect->setSource(QUrl::fromLocalFile(taskItem::config->m_bellCurrent));
    m_timer_end_effect->setLoopCount(taskItem::config->m_bellLoop);
    m_timer_end_effect->setVolume(0.7f);
    m_timer_end_effect->play();
}

